package com.example.yaja.yaja.controller;

import com.example.yaja.yaja.common.BaseResponse;
import com.example.yaja.yaja.common.ResponseBuilder;
import com.example.yaja.yaja.dao.IdeaMapper;
import com.example.yaja.yaja.entity.Idea;
import com.example.yaja.yaja.service.IdeaService;
import com.example.yaja.yaja.service.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.util.*;

/**
 * Created by Zero on 2017/12/4.
 */
@RestController
@RequestMapping("/idea")
public class IdeaController {

    @Autowired
    IdeaService ideaService;
    @Autowired
    UserService userService;

    @Autowired
    private IdeaMapper ideaMapper;


    /**
     * 添加留言
     * @param idea
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public int addUser(@RequestBody Idea idea, HttpSession userId){

        int result=0;
        Object userid=userId.getAttribute("userId");
        if(idea!=null&&userid!=null){
            idea.setTime(new Date());
            idea.setUserid((Integer) userid);
            ideaService.addIdea(idea);
            result=1;
        }else {
            result=0;
        }
        return result;
    }

    /**
     * 展示留言
     * @return
     */
    @RequestMapping(value = "/show",method = RequestMethod.GET)
    public Map<String, Object> findAllIdea(){

        Map<String,Object> map = new HashMap<String, Object>();
        List<Idea> ideas=ideaService.findAllIdea();
        if(ideas!=null){
            for(int i=0;i<ideas.size();i++){
                int userId=ideas.get(i).getUserid();
                String name=userService.findUserById(userId).getName();
                String time= DateFormat.getDateInstance().format(ideas.get(i).getTime());
                ideas.get(i).setTimeA(time);
                ideas.get(i).setUserName(name);
            }
            map.put("ideas",ideas);
        }else {
            map.put("ideasState",0);
        }
        return map;
    }

    /**
     * 分页展示留言
     * @param currPage
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/getAllIdea", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getAllIdea(@RequestParam Integer currPage,
                                   @RequestParam Integer pageSize){
        ResponseBuilder custom = ResponseBuilder.custom();
        List<Idea> ideas=ideaService.findAllIdea();

        if (Objects.isNull(currPage) || currPage == 0) currPage = 1;
        if (Objects.isNull(pageSize) || pageSize == 0) pageSize = 10;
        //分页
        PageHelper.startPage(currPage, pageSize);

        try {
            if(ideas!=null){
                for(int i=0;i<ideas.size();i++){
                    int userId=ideas.get(i).getUserid();
                    String name=userService.findUserById(userId).getName();
                    String time= DateFormat.getDateInstance().format(ideas.get(i).getTime());
                    ideas.get(i).setTimeA(time);
                    ideas.get(i).setUserName(name);
                }
                custom.data(ideas).currPage(currPage).pageSize(pageSize).totalCount(ideas.size());
            }else {
                custom.failed("查询失败", -1);
            }
        } catch (Exception e) {
            custom.failed("查询失败", -1);
        }
        return custom.build();
    }

    /**
     * 删除留言
     * @param ideaId
     * @return
     */
    @RequestMapping(value = "/delIdea", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse delIdea(Integer ideaId){
        ResponseBuilder custom = ResponseBuilder.custom();
        int result=ideaMapper.deleteByPrimaryKey(ideaId);

        if (result!=0){
            custom.success("成功",0);
        }else{
            custom.failed("失败", -1);
        }
        return custom.build();
    }


}
